<template>
  <div>
    <el-dialog title="添加填空题" :visible.sync="dialogVisible">
      <el-form ref="form" :model="form" size="mini">
        <el-form-item label="题目名称" :label-width="formLabelWidth">
          <el-input type="text" v-model="form.title" autocomplete="off" />
        </el-form-item>
        <el-form-item label="题号" :label-width="formLabelWidth">
          <el-input-number
            v-model="form.idRange[0]" controls-position="right" :min="1" :max="form.idRange[1]"/>
          -
          <el-input-number
            v-model="form.idRange[1]" controls-position="right" :min="form.idRange[0]"/>
        </el-form-item>
        <el-form-item label="每行空数" :label-width="formLabelWidth">
          <el-input-number :min="1" :max="5" v-model="form.blankPerLine" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapMutations } from 'vuex';
import _ from 'lodash';
import { QUESTION_GROUP_TYPE_MAP, QUESTION_SIZE_MAP } from '@/assets/js/constant';
import { uuid as createUUID, createHtmlSpace } from '@/assets/js/utils';
import CommonMixin from './common-mixin';

export default {
  mixins: [CommonMixin],
  props: {
    uuid: {
      type: String,
      default: '',
    },
  },
  computed: {
    // 是否在编辑一个已经存在的题组
    isModifyByUUID() {
      return this.uuid !== '';
    },
    submitData() {
      const uuid = createUUID();
      const groupType = QUESTION_GROUP_TYPE_MAP.BLANK;
      const { title, idRange, blankPerLine } = this.form;
      const [startId, endId] = idRange;
      const { titleHeight } = QUESTION_SIZE_MAP;
      const idList = [...new Array(endId - startId + 1)];
      const { itemHeight, paddingBottomHeight } = QUESTION_SIZE_MAP.blank;
      const rowCount = Math.ceil(idList.length / blankPerLine);
      const height = titleHeight + rowCount * itemHeight + paddingBottomHeight;
      const totalHeight = height;
      const spaceHtml = `<u>${createHtmlSpace([106, 50, 32, 23, 17][blankPerLine - 1])}</u>`;
      const contents = _.chunk(
        idList.map((item, index) => startId + index),
        blankPerLine,
      )
        .map(item => item
          .map(item2 => `${createHtmlSpace(3 - item2.toString().length)}${item2}.${createHtmlSpace(1)}${spaceHtml}`)
          .join(''))
        .map(item => `<p>${item}</p>`).join('');
      const noSplitHeight = itemHeight;
      return {
        title,
        uuid,
        groupType,
        startId,
        endId,
        titleHeight,
        partIndex: 0,
        height,
        totalHeight,
        noSplitHeight,

        blankPerLine,
        contents,
      };
    },
  },
  methods: {
    ...mapMutations([
      'updateQuestionGroupItem',
    ]),
    handleSubmit() {
      if (this.isModifyByUUID) {
        this.modifySubmit();
      } else {
        this.submit();
      }
    },
    resetFormData() {
      const beginId = this.getBeginId();
      return {
        title: '填空题',
        idRange: [beginId, beginId],
        blankPerLine: 1,
      };
    },
    modifySubmit() {
      const curQuesGroupItem = this.questionGroupList.find(item => item.uuid === this.uuid);
      const { uuid, height, contents } = curQuesGroupItem;
      const { idRange, blankPerLine } = this.form;
      const [startId, endId] = idRange;
      const idList = [...new Array(endId - startId + 1)];
      const { itemHeight } = QUESTION_SIZE_MAP.blank;
      const rowCount = Math.ceil(idList.length / blankPerLine);
      const addedHeight = rowCount * itemHeight;
      const newHeight = height + addedHeight;
      const spaceHtml = `<u>${createHtmlSpace([106, 50, 32, 23, 17][blankPerLine - 1])}</u>`;
      const addedContents = _.chunk(
        idList.map((item, index) => startId + index),
        blankPerLine,
      )
        .map(item => item
          .map(item2 => `${createHtmlSpace(3 - item2.toString().length)}${item2}.${createHtmlSpace(1)}${spaceHtml}`)
          .join(''))
        .map(item => `<p>${item}</p>`).join('');
      const newContents = `${contents}${addedContents}`;
      this.updateQuestionGroupItem({
        uuid,
        key: 'endId',
        value: endId,
      });
      this.updateQuestionGroupItem({
        uuid,
        key: 'height',
        value: newHeight,
      });
      this.updateQuestionGroupItem({
        uuid,
        key: 'totalHeight',
        value: newHeight,
      });
      this.updateQuestionGroupItem({
        uuid,
        key: 'contents',
        value: newContents,
      });
      this.dialogVisible = false;
    },
  },
};
</script>
